// 引入pinia定义仓库
import {defineStore} from 'pinia'
import {reqGetCategory1IdList,reqGetCategory2IdList,reqGetCategory3IdList} from '@/api/product/category'
// 引入限制
import type {categoryStateModel,categoryListModel} from '@/api/product/model/categoryModel'
// 用defineStore创建模块
export const useCategoryListStore = defineStore('categoryList',{
  state:(): categoryStateModel=> {
    return {
       // 以下三个属性用于收集用户真正选择的分类的id
      category1Id:undefined,
      category2Id:undefined,
      category3Id:undefined,
      // 以下三个数组用于存储服务器响应回来的分类数据
      category1IdList:[],
      category2IdList:[],
      category3IdList:[],
    }
  },
  actions:{ 
    // 1. 获取一级导航数据
    async getCategory1IdList(){
      const result = await reqGetCategory1IdList()
      console.log('store',result);
      this.category1IdList = result
    },

    //  获取二级导航数据
    async getCategory2IdList(id:number){
      this.category1Id = id
      // 一级列表变化时，二级三级id要清空，三级列表也要清空
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3IdList = []
      const result = await reqGetCategory2IdList(id)
      console.log('二级actions',result);
      this.category2IdList = result
      
    },

    //  获取三级导航数据
    async getCategory3IdList(id:number){
      this.category2Id = id
      // 重置三级id
      this.category3Id = undefined
      const result =  await reqGetCategory3IdList(id)
      console.log('三级actions',result);
      this.category3IdList = result
      
    },

    // 设置三级分类id
    setCategory3Id(id:number){
      this.category3Id = id
    }

  }
})